package com.xiyuan.smartutils.template.engine.expression.operator;

import com.xiyuan.smartutils.Types;

import com.xiyuan.smartutils.template.engine.VariableMap;
import com.xiyuan.smartutils.template.engine.exception.ExpressionException;
import com.xiyuan.smartutils.template.engine.expression.Expression;
import com.xiyuan.smartutils.template.engine.expression.operator.arithmetic.impl._Subtraction;

/**
 * 取负运算(-)
 * 
 * @see _Subtraction 和减号 运算符相同，要注意区分
 * @version v1.0.0 @author lgz 2020年12月6日 新建与整理
 */
public class _Negative extends Operator
{
    private static final long serialVersionUID = 1L;
    
    public _Negative(String path,int line, int column, Expression target)
    {
        super(path, line, column);
        this.setTarget(target);
        this.assertSymbo("负号", target);
    }
    
    @Override
    public int getType()
    {
        return NEGATIVE;
    }
    
    @Override
    protected Object build(VariableMap variableMap)
    {
        Object obj = target.execute(variableMap);
        
        if (Types.isByte(obj))
            return -(Byte) obj;
        
        if (Types.isShort(obj))
            return -(Short) obj;
        
        if (Types.isInt(obj))
            return -(Integer) obj;
        
        if (Types.isLong(obj))
            return -(Long) obj;
        
        if (Types.isFloat(obj))
            return -(Float) obj;
        
        if (Types.isDouble(obj))
            return -(Double) obj;
        
        throw new ExpressionException(this, "取负表达式{" + this + "}格式不正确，要求结果不是整数或小数");
    }
    
    @Override
    public String toString()
    {
        return new StringBuilder("-").append(target).toString();
    }
}
